package com.start_eternity.oasissafewalk.controller;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.pojo.*;
import com.start_eternity.oasissafewalk.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cars")
public class CarController {
    @Autowired
    private  CarService carService;
    /*
     * 查询车辆信息
     * */
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,
                       String carName){
        log.info("分页查询,参数:{},{},{}",page,pageSize,carName);
        //调用service方法进行分页查询
        PageBean pageBean= carService.page(page,pageSize,carName);
        return Result.success(pageBean);
    }
    /*删除车辆
     * */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除操作,ids:{}",ids);
        carService.delete(ids);
        return Result.success();
    }
    /*
     * 新增车辆
     * */

    @PostMapping
    public Result add(@RequestBody Car car){
        log.info("新增车辆:{}",car);
        //调用service新增
        carService.add(car);
        return Result.success();
    }
    //修改车辆数据
    @GetMapping("/{carId}")
    public Result getById(@PathVariable Integer carId){
        log.info("根据ID查询车辆信息,:{}",carId);
        Car car=carService.getById(carId);
        return Result.success(car);
    }
    @PutMapping
    public Result update(@RequestBody Car car){
        log.info("更新车辆信息:{}",car);
        carService.update(car);
        return Result.success();
    }
}

